package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.service.IStraregyCommendService;
import cn.wolfcode.luowowo.common.query.StrategyQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.mgrsite.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("strategyCommend")
public class StrategyCommendController {

    @Value("${file.dir}")
    private String dir;

    @Reference
    private IStraregyCommendService straregyCommendService;

    @RequestMapping("list")
    public String query(StrategyQueryObject qo, Model model){
        PageInfo pageInfo = straregyCommendService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategyCommend/list";
    }

    @RequestMapping("save")
    @ResponseBody
    public Object save(StrategyCommend commend, MultipartFile pic){
        if (pic != null && pic.getSize() > 0) {
            String coverUrl = UploadUtil.upload(pic, dir);
            commend.setCoverUrl("/" + coverUrl);
        }
        straregyCommendService.save(commend);
        return new AjaxResult();
    }
}
